let express = require('express');
let router = express.Router();

const interviewService = require('../service/interviewService');
const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');

// 此路由需要登录后才能访问
router.use(checkJWTMiddleWare);

/*
 * 查询所有访谈记录接口
 */
router.get('/getAllInterviewList', async function (req, res, next) {
    // 这个接口要查两个表，并且进行拼接
    let result = await interviewService.getAllInterviewList(req.jwtResult.data.uid, req.jwtResult.data.userType);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 新增日常访谈记录接口
 */
router.post('/addDailyVisitInterview', async function (req, res, next) {
    let studentId = req.body.studentId;
    let startTime = req.body.startTime;
    let endTime = req.body.endTime;
    let teacherId = req.body.teacherId;
    let interviewContent = req.body.interviewContent;
    let isWarning = req.body.isWarning;
    let interviewPhoto = req.body.interviewPhoto;
    if (!studentId || !startTime || !endTime || !teacherId || !interviewContent || !isWarning) {
        res.status(400).json({ success: false, msg: "参数不足，新增失败" });
        return ;
    }
    interviewPhoto = interviewPhoto || '';
    // 判断 interviewPhoto 这个base64字符串占用的字节是否超过 10M, 超过直接返回失败
    if (Buffer.byteLength(interviewPhoto, 'utf8') > 10 * 1024 * 1024) {
        res.status(400).json({ success: false, msg: "图片过大，请重新上传" });
        return ;
    }
    let result;
    if (interviewPhoto) {
        result = await interviewService.addDailyVisitInterview(req.jwtResult.data.uid, studentId, startTime, endTime, teacherId, interviewContent, isWarning, interviewPhoto);
    } else {
        result = await interviewService.addDailyVisitInterviewNoImg(req.jwtResult.data.uid, studentId, startTime, endTime, teacherId, interviewContent, isWarning);
    }
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 新增普查访谈记录接口
 */
router.post('/addCensusVisitInterview', async function (req, res, next) {
    let studentId = req.body.studentId;
    let startTime = req.body.startTime;
    let endTime = req.body.endTime;
    let counselorId = req.body.counselorId;
    let question1 = req.body.question1;
    let question2 = req.body.question2;
    let question3 = req.body.question3;
    let question4 = req.body.question4;
    let question5 = req.body.question5;
    let question6 = req.body.question6;
    let question7 = req.body.question7;
    let note = req.body.note;
    let centerOpinionId = req.body.centerOpinionId;
    let warningLevelId = req.body.warningLevelId;
    
    if (!studentId || !startTime || !endTime || !counselorId || !question1 || !question2 || !question3 || !question4 || !question6 || !centerOpinionId || !warningLevelId) {
        res.status(400).json({ success: false, msg: "参数不足，新增失败" });
        return ;
    }

    let result = await interviewService.addCensusVisitInterview(req.jwtResult.data.uid, studentId, startTime, endTime, counselorId, question1, question2, question3, question4, question5, question6, question7, note, centerOpinionId, warningLevelId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 新增家校沟通访谈记录接口
 */
router.post('/addHomeSchoolInterview', async function (req, res, next) {
    let studentId = req.body.studentId;
    let startTime = req.body.startTime;
    let endTime = req.body.endTime;
    let teacherId = req.body.teacherId;
    let interviewContent = req.body.interviewContent;
    let isWarning = req.body.isWarning;
    let interviewPhoto = req.body.interviewPhoto;
    
    if (!studentId || !startTime || !endTime || !teacherId || !interviewContent || !isWarning) {
        res.status(400).json({ success: false, msg: "参数不足，新增失败" });
        return ;
    }

    interviewPhoto = interviewPhoto || '';
    // 判断 interviewPhoto 这个base64字符串占用的字节是否超过 10M, 超过直接返回失败
    if (Buffer.byteLength(interviewPhoto, 'utf8') > 10 * 1024 * 1024) {
        res.status(400).json({ success: false, msg: "图片过大，请重新上传" });
        return ;
    }
    let result;
    if (interviewPhoto) {
        result = await interviewService.addHomeSchoolInterview(req.jwtResult.data.uid, studentId, startTime, endTime, teacherId, interviewContent, isWarning, interviewPhoto);
    } else {
        result = await interviewService.addHomeSchoolInterviewNoImg(req.jwtResult.data.uid, studentId, startTime, endTime, teacherId, interviewContent, isWarning);
    }
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 修改日常访谈记录接口
 */
router.post('/updateDailyInterview', async function (req, res, next) {
    let interviewId = req.body.interviewId;
    let startTime = req.body.time;
    let teacherId = req.body.teacherId;
    let interviewContent = req.body.interviewContent;
    let isWarning = req.body.isWarning;
    if (!interviewId || !startTime || !teacherId || !interviewContent || !isWarning) {
        res.status(400).json({ success: false, msg: "参数不足，修改失败" });
        return ;
    }
    let result = await interviewService.updateDailyInterview(req.jwtResult.data.uid, req.jwtResult.data.userType, interviewId, startTime, teacherId, interviewContent, isWarning);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 修改普查访谈记录接口
 */
router.post('/updateCensusInterview', async function (req, res, next) {
    let interviewId = req.body.interviewId;
    let startTime = req.body.time;
    let counselorId = req.body.counselorId;
    let question1 = req.body.question1;
    let question2 = req.body.question2;
    let question3 = req.body.question3;
    let question4 = req.body.question4;
    let question5 = req.body.question5;
    let question6 = req.body.question6;
    let question7 = req.body.question7;
    let note = req.body.note;
    let centerOpinionId = req.body.centerOpinionId;
    let warningLevelId = req.body.warningLevelId;
    if (!interviewId || !startTime || !counselorId || !question1 || !question2 || !question3 || !question4 || !question6 || !centerOpinionId || !warningLevelId) {
        res.status(400).json({ success: false, msg: "参数不足，修改失败" });
        return ;
    }
    let result = await interviewService.updateCensusInterview(req.jwtResult.data.uid, req.jwtResult.data.userType, interviewId, startTime, counselorId, question1, question2, question3, question4, question5, question6, question7, note, centerOpinionId, warningLevelId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 修改家校沟通访谈记录接口
 */
router.post('/updateHomeSchoolInterview', async function (req, res, next) {
    let interviewId = req.body.interviewId;
    let startTime = req.body.time;
    let teacherId = req.body.teacherId;
    let interviewContent = req.body.interviewContent;
    let isWarning = req.body.isWarning;
    if (!interviewId || !startTime || !teacherId || !interviewContent || !isWarning) {
        res.status(400).json({ success: false, msg: "参数不足，修改失败" });
        return ;
    }
    let result = await interviewService.updateHomeSchoolInterview(req.jwtResult.data.uid, req.jwtResult.data.userType, interviewId, startTime, teacherId, interviewContent, isWarning);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 删除日常访谈记录接口
 */
router.post('/deleteDailyInterview', async function (req, res, next) {
    let interviewId = req.body.interviewId;
    if (!interviewId) {
        res.status(400).json({ success: false, msg: "参数不足，修改失败" });
        return ;
    }
    let result = await interviewService.deleteDailyInterview(req.jwtResult.data.uid, req.jwtResult.data.userType, interviewId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 删除普查访谈记录接口
 */
router.post('/deleteCensusInterview', async function (req, res, next) {
    let interviewId = req.body.interviewId;
    if (!interviewId) {
        res.status(400).json({ success: false, msg: "参数不足，修改失败" });
        return ;
    }
    let result = await interviewService.deleteCensusInterview(req.jwtResult.data.uid, req.jwtResult.data.userType, interviewId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 删除家校沟通访谈记录接口
 */
router.post('/deleteHomeSchoolInterview', async function (req, res, next) {
    let interviewId = req.body.interviewId;
    if (!interviewId) {
        res.status(400).json({ success: false, msg: "参数不足，修改失败" });
        return ;
    }
    let result = await interviewService.deleteHomeSchoolInterview(req.jwtResult.data.uid, req.jwtResult.data.userType, interviewId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查询某个日常访谈的详细信息
 */
router.post('/getDailyInterviewInfo', async function (req, res, next) {
    let interviewId = req.body.interviewId;
    if (!interviewId) {
        res.status(400).json({ success: false, msg: "参数不足，查询失败" });
        return ;
    }
    let result = await interviewService.getDailyInterviewInfo(req.jwtResult.data.uid, req.jwtResult.data.userType, interviewId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查询某个普查访谈的详细信息
 */
router.post('/getCensusInterviewInfo', async function (req, res, next) {
    let interviewId = req.body.interviewId;
    if (!interviewId) {
        res.status(400).json({ success: false, msg: "参数不足，查询失败" });
        return ;
    }
    let result = await interviewService.getCensusInterviewInfo(req.jwtResult.data.uid, req.jwtResult.data.userType, interviewId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查询某个家校沟通访谈的详细信息
 */
router.post('/getHomeSchoolInterviewInfo', async function (req, res, next) {
    let interviewId = req.body.interviewId;
    if (!interviewId) {
        res.status(400).json({ success: false, msg: "参数不足，查询失败" });
        return ;
    }
    let result = await interviewService.getHomeSchoolInterviewInfo(req.jwtResult.data.uid, req.jwtResult.data.userType, interviewId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查询某个学生的所有访谈记录
 */
router.post('/getInterviewByStudent', async function (req, res, next) {
    let studentId = req.body.studentId;
    if (!studentId) {
        res.status(400).json({ success: false, msg: "参数不足，查询失败" });
        return ;
    }
    let result = await interviewService.getInterviewByStudent(req.jwtResult.data.uid, req.jwtResult.data.userType, studentId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查询所有中心意见接口
 */
router.get('/getAllCenterOpinion', async function (req, res, next) {
    let result = await interviewService.getAllCenterOpinion();
    res.status(result.success ? 200 : 400).json(result);
});

module.exports = router;